﻿using System;
using System.Xml.Linq;
using Schema = QASPRibbon.RibbonSchema;

namespace QASPRibbon.Controls
{
    /// <summary>
    /// Represents a CheckBox control in the SharePoint Server Ribbon.
    /// </summary>
    public class RibbonCheckBox : RibbonButton
    {
        #region Properties

        /// <summary>
        /// Gets or sets a value indicating whether the CheckBox control is checked.
        /// </summary>
        public bool Checked { get; set; }

        internal override string XmlTag
        {
            get { return Schema.CheckBox; }
        }

        #endregion

        #region Constructors

        /// <summary>
        /// Creates and initializes a new instance of the RibbonCheckBox.
        /// </summary>
        public RibbonCheckBox() { }

        /// <summary>
        /// Creates and initializes a new instance of the RibbonCheckBox.
        /// </summary>
        /// <param name="id">Identifier</param>
        public RibbonCheckBox(string id) : base(id) { }

        /// <summary>
        /// Creates and initializes a new instance of the RibbonCheckBox.
        /// </summary>
        /// <param name="id">Identifier</param>
        /// <param name="title">Title</param>
        public RibbonCheckBox(string id, string title) : base(id, title) { }

        #endregion

        #region Internal

        internal override XElement AsXElement()
        {
            var res = base.AsXElement();
            res.Add(new XAttribute(Schema.QueryCommand, FullId + Schema.QueryCommand));
            return res;
        }

        #endregion

        #region PostBack Action

        ///<summary>
        /// Occurs when the CheckBox control is clicked.
        ///</summary>
        public new event RibbonCheckBoxActionEventHandler OnAction;

        internal void DoOnAction(RibbonCheckBoxEventArgs e)
        {
            var handler = OnAction;
            if (handler != null) handler(this, e);
        }

        #endregion
    }

    /// <summary>
    /// RibbonCheckBox event handler
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e">Event arguments</param>
    public delegate void RibbonCheckBoxActionEventHandler(object sender, RibbonCheckBoxEventArgs e);

    /// <summary>
    /// Arguments of server ation rised by RibbonButton
    /// </summary>
    public class RibbonCheckBoxEventArgs : EventArgs
    {
        /// <summary>
        /// Instance of RibbonButton
        /// </summary>
        public RibbonCheckBox Instance;
        /// <summary>
        /// Checked (True) or not (False)
        /// </summary>
        public bool Checked;

        internal RibbonCheckBoxEventArgs(RibbonCheckBox button, bool checkedFlag)
        {
            Instance = button;
            Checked = checkedFlag;
        }
    }
}
